package com.wbc.smartHome.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wbc.smartHome.dao.CommodityLabelDao;
import com.wbc.smartHome.model.CommodityLabel;
import com.wbc.smartHome.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class CommodityLabelService {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CommodityLabelDao commodityLabelDao;
    private String key = "commodity";

    public PageInfo<CommodityLabel> getCommodityLabel(CommodityLabel commodityLabel) {
       /* ListOperations listOps  = redisTemplate.opsForList();
        List<Commodity> commodityList = listOps.range(key, 0, -1);*/
        PageHelper.startPage(commodityLabel.getPageNo(),commodityLabel.getPageSize());
        List<CommodityLabel> CommodityLabelList = commodityLabelDao.getCommodityLabelList(commodityLabel);
        PageInfo<CommodityLabel> pageInfo = new PageInfo<>(CommodityLabelList);
        return pageInfo;
    }


    public void setCommodityLabel(CommodityLabel commodityLabel,String adminToken) {
      /*ListOperations listOps  = redisTemplate.opsForList();
        listOps.rightPush(key, commodity);*/
        int id = JWTUtil.getAdmin(adminToken).getId();
        commodityLabelDao.setCommodityLabel(commodityLabel.getName(),id);
    }


    public void delete(int id) {
        commodityLabelDao.delete(id);
    }

    public CommodityLabel getCommodityLabelById(int id) {
        CommodityLabel commodityLabel = commodityLabelDao.getCommodityLabelById(id);
        return commodityLabel;
    }

    public void updateCommodityLabel(CommodityLabel commodityLabel, String adminToken) {
        int adminId = JWTUtil.getAdmin(adminToken).getId();
        commodityLabelDao.updateCommodityLabel(commodityLabel.getId(),commodityLabel.getName(),adminId);
    }
}

